/**
 * 
 */
package com.zufangbao.earth.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zufangbao.earth.service.AuthorisedBillSketchService;
import com.zufangbao.gluon.api.quark.AuthorisedBillSketch;
import com.zufangbao.gluon.api.quark.BillStatus;
import com.zufangbao.gluon.exception.CommonException;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4AutorisedBillSketch;
import com.zufangbao.gluon.spec.global.GlobalMsgSpec;
import com.zufangbao.sun.entity.order.Order;
import com.zufangbao.sun.entity.order.OrderStatus;
import com.zufangbao.sun.service.OrderService;

/**
 * @author wukai
 *
 */
@Service("authorisedBillSketchService")
public class AuthorisedBillSketchServiceImpl implements AuthorisedBillSketchService{
	
	@Autowired
	private OrderService orderService;

	@Override
	public List<AuthorisedBillSketch> convertOrderToAuthoriseBillSketch(
			List<Order> orderList) throws CommonException {
		
		if(CollectionUtils.isEmpty(orderList)){
			
			throw new CommonException(com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ERROR_EMPTY_APPID, GlobalMsgSpec.GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		List<AuthorisedBillSketch> authorisedBillSketchs = new ArrayList<AuthorisedBillSketch>();
		
		String appId = orderList.get(0).getContract().getApp().getAppId();
				
		for(Order item : orderList){
			
			AuthorisedBillSketch authorisedBillSketch = new AuthorisedBillSketch(appId, item.getTotalRent(), BillStatus.fromOrdinal(item.getOrderStatus().ordinal()), item.getPaidRent(), item.getDueDate(), item.getOrderNo(), item.getId());
			
			authorisedBillSketchs.add(authorisedBillSketch);
		}
		return authorisedBillSketchs;
	}

	@Override
	public List<String> getUniuqueBillIds(List<Order> orderList) throws CommonException {
		
		List<AuthorisedBillSketch> authorisedBillSketchs = convertOrderToAuthoriseBillSketch(orderList);
		
		List<String> uniqueBillIds = new ArrayList<String>();
		
		for(AuthorisedBillSketch item : authorisedBillSketchs){
			
			try {
				
				uniqueBillIds.add(item.generateUniqueBillId());
				
			} catch (Exception e) {
			
				throw new CommonException(com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4AutorisedBillSketch.ERROR_GENERATE_BILL_UNIQUEID,GlobalMsgSpec.GeneralErrorMsg.MSG_ILLEGAL_PARAM);
			}
		}
		
		return uniqueBillIds;
	}

	@Override
	public List<String> updateOrderByHotBillSketchs(
			List<AuthorisedBillSketch> hotBillSketchs) throws CommonException{
		
		if(CollectionUtils.isEmpty(hotBillSketchs)){
			
			throw new CommonException(com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4AutorisedBillSketch.ERROR_EMPTY_HOT_BILL_SKETCHS);
		}
		
		Order order = null;
		
		List<String> uniqueBillIds = new ArrayList<String>();
		
		for(AuthorisedBillSketch item : hotBillSketchs){
			
			order = orderService.getOrder(item.getOutLierOrderNo());
			
			if(null == order){
				
				throw new CommonException(com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4AutorisedBillSketch.ERROR_NOT_EXIST_ORDER);
			}
			
			order.setOrderStatus(OrderStatus.fromValue(item.getBillStatus().getOrdinal()));
			order.setPaidRent(item.getPaidAmount());
			order.setUserUploadParam(item.getUserUpLoadParam());

			orderService.saveOrUpdate(order);
			
			try {
				
				uniqueBillIds.add(item.generateUniqueBillId());
				
			} catch (Exception e) {
				
				throw new CommonException(ErrorCode4AutorisedBillSketch.ERROR_GENERATE_BILL_UNIQUEID);
			}
			
		}
		return uniqueBillIds;
		
	}

}
